package com.leet.code.lcof;

public class Code53_1 {
    public static void main(String[] args) {

        Code53_1 code53_1 = new Code53_1();
        int[] nums = {0, 0, 1, 2, 3, 3, 4};
        int i = code53_1.search(nums, 2);
        System.out.println(i);
    }

    public int search(int[] nums, int target) {

        if (nums.length == 0) {
            return 0;
        }

        int start = 0;
        int end = nums.length - 1;

        int mid = 0;
        while (start < end) {
            mid = start + ((end - start) / 2);

            if (nums[mid] == target) {
                break;
            }
            if (target < nums[mid]) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }

        }
        if (nums[mid] != target) {
            mid = start + ((end - start) / 2);
        }

        int sum = 0, m = mid + 1;
        while (m < nums.length && nums[m] == target) {
            sum++;
            m++;
        }
        while (mid >= 0 && nums[mid] == target) {
            sum++;
            mid--;
        }

        return sum;

    }


}
